/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* cur=new ListNode;
        ListNode* ans=cur;
        while(l1!=nullptr && l2!=nullptr){
            if(l1->val<=l2->val){
                cur->next=new ListNode(l1->val);
                l1=l1->next;
            }
            else{
                cur->next=new ListNode(l2->val);
                l2=l2->next;
            }
            cur=cur->next;
        }
        while(l1!=nullptr){
            cur->next=new ListNode(l1->val);
            l1=l1->next;
            cur=cur->next;
        }
        while(l2!=nullptr){
            cur->next=new ListNode(l2->val);
            l2=l2->next;
            cur=cur->next;
        }
        
        return ans->next;
    }
};